//
//  main.m
//  链表
//
//  Created by clz on 2019/6/27.
//  Copyright © 2019年 clz. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //链表 里面一个data数据   一个next指针
        
        //结构体
        struct p{
            int a;
            struct p *next;
        };   //这个就是我们要的节点
        
        //头   中间的节点    表尾
        //创建
        struct p *head;
        struct p *point;
        struct p *end;
        head = (struct p*)malloc(sizeof(struct p));
        end = head;//如果链表是空表，那么头和尾是相同的
        //我们需要保持最后一个节点始终是end
        
        for (int i = 0; i<4; i++) {
            point = (struct p*)malloc(sizeof(struct p));
            point->a = i;
            end->next = point;
            end = point;
        }
        end->next = NULL;
        
        //head 就是我们要的链表
        
        //修改一个节点数据的值
        int m = 2;
        struct p *temp =head;
        for (int i = 0; i<m; i++) {
            temp = temp->next;
        }
        temp->a = 10;
        
        
        
        //增加一个节点
        int n = 3;
        struct p *addTemp = head;
        for (int i = 0; i<n; i++) {
            addTemp = addTemp->next;
        }
        struct p *newPoint;
        newPoint = (struct p*)malloc(sizeof(struct p));
        newPoint->a = 99;
        newPoint->next = addTemp->next;
        addTemp->next = newPoint;
        
        
        
        
//        删除一个节点
        int k = 3;
        struct p *deleteTemp = head;
        struct p *preTemp = NULL;
        for (int i = 0; i<k; i++) {
            preTemp = deleteTemp;
            deleteTemp = deleteTemp->next;
        }
        preTemp->next = deleteTemp->next;
        free(deleteTemp);

        while (head->next != NULL) {
            head = head->next;
            printf("%d\n",head->a);
        }
        
    }
    return 0;
}
